package cn.zwk.controller;

import cn.zwk.common.model.PageR;
import cn.zwk.common.model.R;
import cn.zwk.domain.system.model.dto.RoleDto;
import cn.zwk.domain.system.model.query.RolePageQuery;
import cn.zwk.domain.system.model.vo.RolePageVo;
import cn.zwk.domain.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping("/page")
    public PageR<RolePageVo> listPaged(RolePageQuery queryParams) {
        return PageR.ok(roleService.listPaged(queryParams));
    }

    @GetMapping("/{roleId}/form")
    public R<RoleDto> getRoleForm(@PathVariable Long roleId) {
        return R.ok(roleService.getInfo(roleId));
    }

    @GetMapping("/{roleId}")
    public R<RoleDto> test(@PathVariable Long roleId) {
        return R.ok(roleService.getInfo(roleId));
    }


    @PostMapping
    public R add(@RequestBody RoleDto roleDto) {
        return R.ok(roleService.save(roleDto));
    }

    @PutMapping
    public R edit(@RequestBody RoleDto roleDto) {
        return R.ok(roleService.update(roleDto));
    }

    /**
     * 删除角色
     * @param ids：id集合
     * @param force：是否强制删除
     * @return
     */
    @DeleteMapping("/{ids}")
    public R del(@PathVariable List<Long> ids,Boolean force) {
        return R.ok(roleService.remove(ids,force));
    }

    @PutMapping("/{roleId}/menus")
    public R assignMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        return R.ok(roleService.assignMenus(roleId, menuIds));
    }

    @GetMapping("/{roleId}/menuIds")
    public R<List<Long>> getRoleMenuIds(@PathVariable Long roleId) {
        return R.ok(roleService.getRoleMenuIds(roleId));
    }
}
